package com.sitem.appsjam.Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class Utils {

    public static String convertStreamToString (InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8*1024);
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return sb.toString();

    }

    public static float distance (GeoPoint a, GeoPoint b) {
    	Location la = new Location("");
    	la.setLatitude(a.getLatitudeE6()/1000000);
    	la.setLongitude(a.getLongitudeE6()/1000000);
    	Location lb = new Location("");
    	lb.setLatitude(b.getLatitudeE6()/1000000);
    	lb.setLongitude(b.getLongitudeE6()/1000000);
    	return la.distanceTo(lb);
    }

    static public Location getLocFromXY(float x_, float y_inverted, MapView map, float orientation){
     	double screenYsize = map.getHeight();
     	double screenXsize = map.getWidth();
     	GeoPoint center = map.getMapCenter();
     	double latitudeYsize = map.getLatitudeSpan();
     	double longitudeXsize = map.getLongitudeSpan();
     	double x = x_;
     	double y = screenYsize - y_inverted;
     	
     	if (orientation != 0){
         	// rotation problem avoiding
     		double dif_x = x - screenXsize/2;
     		double dif_y = y - screenYsize/2;
     		
     		x = dif_x * Math.cos(Math.toRadians(orientation)) + dif_y * Math.sin(Math.toRadians(orientation)) + screenXsize/2;
     		y = dif_y * Math.cos(Math.toRadians(orientation)) - dif_x * Math.sin(Math.toRadians(orientation)) + screenYsize/2;
     	}
     	
     	
//     	float y = (float) (((float)screenYsize) - y_inverted);
     	//Obtaining how many degrees of Latitude and Longitude has a single pixel
     	double pix_Xlongitude = longitudeXsize/screenXsize;
     	double pix_Ylatitude = latitudeYsize/screenYsize;
     	
     	//Obtaining the distance (in microdegrees) to the center
     	double pixcenter_lat = screenYsize/2;
     	double pixcenter_long = screenXsize/2;
     	
     	double degrees_lat = ((y - pixcenter_lat)*pix_Ylatitude);
     	double degrees_long = ((x - pixcenter_long)*pix_Xlongitude);
     	
     	//Obtaining the coordinates the user has tapped
     	double lat = (degrees_lat + (double) center.getLatitudeE6())/1E6;
     	double lng = (degrees_long + (double) center.getLongitudeE6())/1E6;
     	
     	Location loc = new Location("MANUAL_PROVIDER");
     	loc.setLatitude(lat);
     	loc.setLongitude(lng);
     	
     	return loc;
     }
     
}
